package com.sc.nft.admin.controller;

import com.alibaba.excel.EasyExcel;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.dto.UserPropsRewardsDetailReleasedQueryDTO;
import com.sc.nft.entity.vo.UserPropsReleasedDetailVO;
import com.sc.nft.service.MallUserPropsReleasedDetailService;
import com.sc.nft.sup.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/props/released/detail")
@Api(tags = "道具奖励释放记录模块")
public class MallUserPropsReleasedDetailController {

    private final MallUserPropsReleasedDetailService mallUserPropsReleasedDetailService;


    @PostMapping("/page")
    @ApiOperation("分页查询已释放明细记录")
    public PageResult<UserPropsReleasedDetailVO> page(@RequestBody UserPropsRewardsDetailReleasedQueryDTO userPropsRewardsDetailReleasedQueryDTO) {
        return PageResult.success(mallUserPropsReleasedDetailService.page(
                userPropsRewardsDetailReleasedQueryDTO.getStartTime(),
                userPropsRewardsDetailReleasedQueryDTO.getEndTime(),
                userPropsRewardsDetailReleasedQueryDTO.getPhoneNumber(),
                userPropsRewardsDetailReleasedQueryDTO.toMp()));
    }


    @SneakyThrows
    @PostMapping(value = "/export", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "已释放明细记录导出excel", notes = "已释放明细记录导出excel", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public void export(@RequestBody UserPropsRewardsDetailReleasedQueryDTO userPropsRewardsDetailReleasedQueryDTO, HttpServletResponse response) {
        userPropsRewardsDetailReleasedQueryDTO.setPageSize(Integer.MAX_VALUE);
        List<UserPropsReleasedDetailVO> data = mallUserPropsReleasedDetailService.page(
                userPropsRewardsDetailReleasedQueryDTO.getStartTime(),
                userPropsRewardsDetailReleasedQueryDTO.getEndTime(),
                userPropsRewardsDetailReleasedQueryDTO.getPhoneNumber(),
                userPropsRewardsDetailReleasedQueryDTO.toMp()).getRecords();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("已释放用户道具奖励明细", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), UserPropsReleasedDetailVO.class).sheet("已释放用户道具奖励明细").doWrite(data);
    }
}
